﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections.Specialized;
using System.Web;

namespace GtsServer.Service.Model.Common
{
    public class CallBackAlipayModel
    {
        public CallBackAlipayModel(Stream input, out NameValueCollection qs)
        {
            try
            {
                SetObjectValue(this, input, out qs);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public CallBackAlipayModel(string input, out NameValueCollection qs)
        {
            try
            {
                SetObjectValue(this, input, out qs);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public string notify_data { get; set; }
        public string sign { get; set; }
        public static void SetObjectValue(CallBackAlipayModel request, Stream input, out NameValueCollection qs)
        {
            string param = "";
            if (input == null)
            {
                throw new ArgumentNullException("POST参数为NULL");
            }
            try
            {
                using (StreamReader sr = new StreamReader(input))
                {
                    param = sr.ReadToEnd();
                    //LogWrite.WriteLog(param, "支付宝回调参数");
                    qs = HttpUtility.ParseQueryString(param);
                    foreach (var item in request.GetType().GetProperties())
                    {
                        object value;
                        switch (item.PropertyType.Name.ToUpper())
                        {
                            case "STRING":
                                value = qs[item.Name];
                                break;
                            case "INT32":
                                value = Convert.ToInt32(qs[item.Name]);
                                break;
                            case "INT64":
                                value = Convert.ToInt64(qs[item.Name]);
                                break;
                            default:
                                value = qs[item.Name];
                                break;
                        }
                        item.SetValue(request, value, null);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Post参数值转换失败.参数：" + param, ex);
            }
        }

        public static void SetObjectValue(CallBackAlipayModel request, string input, out NameValueCollection qs)
        {
            string param = "";
            if (input == null)
            {
                throw new ArgumentNullException("POST参数为NULL");
            }
            try
            {
                using (StreamReader sr = new StreamReader(input))
                {
                    param = sr.ReadToEnd();
                    qs = HttpUtility.ParseQueryString(param);
                    foreach (var item in request.GetType().GetProperties())
                    {
                        object value;
                        switch (item.PropertyType.Name.ToUpper())
                        {
                            case "STRING":
                                value = qs[item.Name];
                                break;
                            case "INT32":
                                value = Convert.ToInt32(qs[item.Name]);
                                break;
                            case "INT64":
                                value = Convert.ToInt64(qs[item.Name]);
                                break;
                            default:
                                value = qs[item.Name];
                                break;
                        }
                        item.SetValue(request, value, null);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Post参数值转换失败.参数：" + param, ex);
            }
        }

    }
}
